﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using FlowEngine;
using GDI_AnalysisComponents.Properties;

namespace GDI_AnalysisComponents
{
	public partial class MainForm : Form
	{
		//定义变量
		private GGraph ggraph = new GGraph();
		private GButtonDown gbd = new GButtonDown();
		private GMouseMove gmm = new GMouseMove();
		private GMouseUp gmu = new GMouseUp();
		private MoveGComponent mgc = new MoveGComponent();
		private CreateGLine createGLine = new CreateGLine();
		private DCommand dcommand = new DCommand();
		private Control cl;
		public MainForm()
		{
			InitializeComponent();
		}

		private void Init()
		{
			AddComponent();
			cl = this.Middle_PictureBox;

			this.OutXMLToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.SaveToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.OpenToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.SeqToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.LinkLineToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.DeletLineToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.DelAllLineToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.DeleteComponentToolStripButton.Click += new System.EventHandler(HandleToolBar);
			this.NewFileToolStripButton.Click += new System.EventHandler(HandleToolBar);


			Left_toolStripStatusLabel.Text = "应用程序已启动！";

			toolStrip.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			menuStrip.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			Attribute_groupBox.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			GcomponentGroupBox.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			SeqGroupBox.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			statusStrip.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);
			Middle_PictureBox.MouseMove += new MouseEventHandler(HandleMouseMoveEvent);

			//toolStrip.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//menuStrip.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//Attribute_groupBox.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//GcomponentGroupBox.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//SeqGroupBox.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//statusStrip.MouseEnter += new EventHandler(HandleMouseEnterEvent);
			//Middle_PictureBox.MouseEnter += new EventHandler(HandleMouseEnterEvent);

			toolStrip.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			menuStrip.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			Attribute_groupBox.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			GcomponentGroupBox.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			SeqGroupBox.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			statusStrip.MouseUp += new MouseEventHandler(HandleMouseUpEvent);
			Middle_PictureBox.MouseUp += new MouseEventHandler(HandleMouseUpEvent);

		}
		private void MainForm_Load(object sender, EventArgs e)
		{
			Init();
		}

		//添加组件
		public void AddComponent()
		{
			//lexus add at 2008.12.19
			//载入所有plugins目录下的dll文件,对符合要求的dll进行载入操作
			//string path = @"D:\wwwroot\OICF\eHunter\RegexExtractRule\bin\Debug\Component.RegexExtractRule.dll";
			string[] ss = Directory.GetFiles(Path.Combine(Environment.CurrentDirectory, "plugins"), "*.dll");
			foreach (string path in ss)
				LoadComponent(path);


			//GComponent gc = new GComponent("北京大学", "xxx");
			//gc.AddParaGUnit("name", "string");
			//gc.AddParaGUnit("sex", "string");
			//gc.AddParaGUnit("Age", "int");

			//GComponent gc1 = new GComponent("工业大学", "yyy");
			//gc1.AddParaGUnit("name", "string");
			//gc1.AddParaGUnit("sex", "string");
			//gc1.AddParaGUnit("Age", "int");

			//GComponent gc2 = new GComponent("MAMAMAMAMAMAMA", "zzz");
			//gc2.AddParaGUnit("ID", "string");
			//gc2.AddParaGUnit("Class", "string");
			//gc2.AddParaGUnit("Num", "int");

			//ggraph.Gcs.Add(gc);
			//ggraph.Gcs.Add(gc1);
			//ggraph.Gcs.Add(gc2);


			//使用资源
			//this.Middle_PictureBox.Image = GDI_AnalysisComponents.Properties.Resources.zujian;

			ImageList imageListSmall = new ImageList();
			imageListSmall.Images.Add((Image)(GDI_AnalysisComponents.Properties.Resources.zujian1));

			ComponentListView.SmallImageList = imageListSmall;

			for (int i = 0; i < ggraph.Gcs.Count; i++)
			{
				string text = ((GComponent)(ggraph.Gcs[i])).Name;

				ListViewItem lvItem = new ListViewItem("" + (i + 1) + "." + text, 0);

				ComponentListView.Items.Add(lvItem);

			}

		}

		private void LoadComponent(string path)
		{
			Assembly o = Assembly.LoadFrom(path);
			Type[] oTypes = o.GetTypes();
			ComponentAttribute[] attrs;
			foreach (Type t in oTypes)
			{
				List<Type> list = new List<Type>();
				list.Add(t);
				list.AddRange(t.GetInterfaces());
				for (int i = 0; i < list.Count; i++)
				{
					attrs = (ComponentAttribute[])list[i].GetCustomAttributes(typeof(ComponentAttribute), false);
					if (attrs.Length > 0)
					{
						for (int j = 0; j < attrs.Length; j++)
						{
							GComponent gc = new GComponent(attrs[i].Description, attrs[i].Id);
							//GComponent gc = new GComponent("北京大学", "xxx");
							Console.WriteLine(attrs[i].Id);
							Console.WriteLine(attrs[i].Company);
							PropertyInfo[] pis = list[i].GetProperties(BindingFlags.Public | BindingFlags.Instance);
							for (int k = 0; k < pis.Length; k++)
							{
								//Console.WriteLine(pis[k].Name);
								//Console.WriteLine(pis[k].PropertyType.ToString());
								gc.AddParaGUnit(pis[k].Name, pis[k].PropertyType.ToString());

							}
							//判断是否还父接口,如果有,输出父接口的公有实例属性
							Type[] subs = list[i].GetInterfaces();
							if (subs != null)
							{
								foreach (Type tt in subs)
								{
									foreach (PropertyInfo pi in tt.GetProperties(BindingFlags.Public | BindingFlags.Instance))
									{
										//Console.WriteLine(pi.Name);
										//Console.WriteLine(pi.PropertyType.ToString());
										gc.AddParaGUnit(pi.Name, pi.PropertyType.ToString());

									}
								}
							}
							ggraph.Gcs.Add(gc);
						}


					}
				}
			}
		}


		//PictureBox的Paint事件
		private void Middle_PictureBox_Paint(object sender, PaintEventArgs e)
		{
			Graphics g = e.Graphics;
			g.SmoothingMode = SmoothingMode.AntiAlias; //
			g.SmoothingMode = SmoothingMode.HighQuality;//绘图模式默认为粗糙模式，将会出现锯齿！
			ggraph.DrawGraphics(g);
		}

		//鼠标点击事件
		private void Middle_PictureBox_MouseDown(object sender, MouseEventArgs e)
		{
			Middle_PictureBox.Select();
			string s = gbd.HandleGButtonDown(e, cl, ggraph, mgc, createGLine);
			Left_toolStripStatusLabel.Text = s;
			PointF mousePt = new PointF(e.X, e.Y);
			ArrayList gcSavs = ggraph.GcSavs;
			GComponent gc = gbd.GetClickGC(gcSavs, mousePt);
			GUnit gun = gbd.GetClickParaGUnit(gcSavs, mousePt);

			if (s == null && gc != null)
			{
				this.IDTextBox.Text = "" + gc.ID;
				this.NameTextBox.Text = gc.Name;
				this.TypeTextBox.Text = gc.Type;
				this.FartherIDTextBox.Text = "";
				Left_toolStripStatusLabel.Text = "你点击了" + gc.Name + "组件！";
			}
			if (s == null && gun != null)
			{
				this.IDTextBox.Text = "" + gun.ID;
				this.NameTextBox.Text = gun.Name;
				this.TypeTextBox.Text = gun.Type;
				this.FartherIDTextBox.Text = "" + gun.FartherGUnitID;
				Left_toolStripStatusLabel.Text = "你点击了" + gun.Name + "单元！";
			}
		}


		//鼠标在PictureBox上移动
		private void Middle_PictureBox_MouseMove(object sender, MouseEventArgs e)
		{
			gmm.HandleGMouseMove(e, cl, ggraph, mgc, createGLine);
			//设置图标图片
			//this.Cursor = new Cursor("pen_r.cur");
		}

		private void Middle_PictureBox_MouseUp(object sender, MouseEventArgs e)
		{
			this.Cursor = System.Windows.Forms.Cursors.Default;
			string s = gmu.HandleGMouseUp(e, cl, ggraph, ComponentListView);
			if (s != null && s != "")
				Left_toolStripStatusLabel.Text = s;
		}


		private void ComponentListView_MouseMove(object sender, MouseEventArgs e)
		{
			if (ggraph.MEM == MouseEventMode.AddComponent)
			{
				this.Cursor = System.Windows.Forms.Cursors.No;
			}
		}



		//删除组件
		private void Middle_PictureBox_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			Keys keys = e.KeyData;
			switch (keys)
			{
				case Keys.Delete:
					string s1 = null;
					string s2 = null;
					s1 = dcommand.DeleteGcomponent(ggraph);
					s2 = dcommand.DeleGLinkLine(ggraph);
					if (s1 != null)
						Left_toolStripStatusLabel.Text = s1;
					if (s2 != null)
						Left_toolStripStatusLabel.Text = s2;
					this.Middle_PictureBox.Invalidate();
					break;
			}
		}


		private void ComponentListView_MouseDown(object sender, MouseEventArgs e)
		{
			ggraph.MEM = MouseEventMode.AddComponent;
		}

		private void ComponentListView_MouseUp(object sender, MouseEventArgs e)
		{
			ggraph.MEM = MouseEventMode.Normal;
			this.Cursor = System.Windows.Forms.Cursors.Default;
		}


		public void HandleToolBar(object sender, EventArgs e)
		{
			switch (((ToolStripButton)sender).Name)
			{
				case "OutXMLToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.OutXML(this, ggraph);
					break;
				case "SaveToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.SaveFile(this, ggraph);
					break;
				case "OpenToolStripButton":
					string s = null;
					ggraph = dcommand.OpenFile(this, ggraph, ref s);
					Left_toolStripStatusLabel.Text = s;
					ggraph.MEM = MouseEventMode.Normal;
					Graphics g = cl.CreateGraphics();
					Middle_PictureBox.Invalidate();
					break;
				case "LinkLineToolStripButton":
					this.ggraph.MEM = MouseEventMode.LinkLine;
					this.Cursor = new Cursor("pen_r.cur");
					break;
				case "DeletLineToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.DeleGLinkLine(ggraph);
					Middle_PictureBox.Invalidate();
					break;
				case "DelAllLineToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.DeleAllGLinkLine(ggraph);
					Middle_PictureBox.Invalidate();
					break;
				case "DeleteComponentToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.DeleteGcomponent(ggraph);
					Middle_PictureBox.Invalidate();
					break;
				case "NewFileToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.NewFile(ggraph);
					Middle_PictureBox.Invalidate();
					break;
				case "SeqToolStripButton":
					Left_toolStripStatusLabel.Text = dcommand.OutSeqFile(this, ggraph);
					ArrayList gcSeqLsit = ggraph.GcSeqLsit;
					SeqListView.Items.Clear();
					SeqListView.Columns.Add("No", 30, HorizontalAlignment.Left);
					SeqListView.Columns.Add("组件名称", -2, HorizontalAlignment.Left);
					if (gcSeqLsit != null)
						for (int i = 0; i < gcSeqLsit.Count; i++)
						{
							int num = i + 1;
							GComponent gc = (GComponent)gcSeqLsit[i];
							string text = gc.Name;
							ListViewItem lvItem = new ListViewItem(new string[] { "" + num, "" + gc.ID + " " + text });
							SeqListView.Items.AddRange(new ListViewItem[] { lvItem });
						}
					break;
			}
			cl.Invalidate();
		}

		public void HandleMouseMoveEvent(object sender, MouseEventArgs e)
		{
			switch (((Control)sender).Name)
			{
				case "toolStrip":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "menuStrip":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Attribute_groupBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "GcomponentGroupBox":
					// this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "SeqGroupBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "statusStrip":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Middle_PictureBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
			}
		}

		public void HandleMouseEnterEvent(object sender, EventHandler e)
		{
			switch (((Control)sender).Name)
			{

				case "toolStrip":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "menuStrip":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Attribute_groupBox":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "GcomponentGroupBox":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "SeqGroupBox":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "statusStrip":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Middle_PictureBox":
					this.Cursor = System.Windows.Forms.Cursors.No;
					break;
			}
			cl.Invalidate();
		}

		public void HandleMouseUpEvent(object sender, MouseEventArgs e)
		{
			switch (((Control)sender).Name)
			{

				case "toolStrip":
					// this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "menuStrip":
					// this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Attribute_groupBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "GcomponentGroupBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "SeqGroupBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "statusStrip":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
				case "Middle_PictureBox":
					//this.Cursor = System.Windows.Forms.Cursors.No;
					break;
			}
			cl.Invalidate();
		}

	}
}
